package com.autonavi.yunda.yunji.core.model.mappers;

import com.autonavi.yunda.yunji.core.model.dto.FunctionConfigDTO;
import com.autonavi.yunda.yunji.core.model.dto.FunctionProjectMappingDTO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * @author sunzhenbang
 */
@Mapper
public interface FunctionProjectMappingMapper extends BaseMapper<FunctionProjectMappingDTO> {
    @SuppressWarnings("all")
    Integer insertBatchSomeColumn(@Param("list") List<FunctionProjectMappingDTO> functionProjectMappingDTOS);

    @Select("select function_id from yunji_function_project_mapping where project_id = #{projectId}")
    List<Long> listDatasourceIdsByPid(@Param("projectId") String projectId);

    @Select({"<script>",
            " SELECT function_id, project_id FROM yunji_function_project_mapping WHERE project_id in ",
            "<foreach item='item' index='index' collection='projectIds' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<FunctionProjectMappingDTO> listByProjectIds(@Param("projectIds") List<String> projectIds);

    @Select("select id, function_id, project_id from yunji_function_project_mapping where function_id = #{functionId}")
    List<FunctionProjectMappingDTO> listFunctionByFId(@Param("functionId") Long functionId);

    /**
     * 根据项目id查询完整数据源
     * @param projectId     项目id
     * @return  符合条件的完整数据源内容
     */
    @Select({"<script>",
            " SELECT distinct con.* ",
            " FROM yunji_function_project_mapping as map, yunji_function_config as con ",
            " WHERE 1=1 ",
            " and map.function_id = con.id ",
            " and con.status = #{status} ",
            " <if test='projectId != null' >",
            "   and map.project_id = #{projectId} ",
            " </if> ",
            "</script>"})
    List<FunctionConfigDTO> listFunctionByProjectId(@Param("projectId") String projectId,
                                                    @Param("status") String status);
}
